package org.hamster.common.utils
{
	import flash.display.BitmapData;
	import flash.geom.ColorTransform;
	
	import mx.core.UIComponent;

	public class CompUtil
	{
		public static function rotate(comp:UIComponent, rotateValue:Number, centerX:Number = NaN, centerY:Number = NaN):void
		{
			if (!comp) {
				return;
			}
			var compW:Number = comp.width / 2;
			var compH:Number = comp.height / 2;
			if (isNaN(centerX)) {
				centerX = compW;
			}
			
			if (isNaN(centerY)) {
				centerY = compH;
			}
			var radVal:Number = Math.PI * rotateValue / 180;
			var newX:Number;
			var newY:Number;
			
			newX = centerX - compW * Math.cos(radVal) + compH * Math.sin(radVal);
			newY = centerY - compW * Math.sin(radVal) - compH * Math.cos(radVal);
			newX = Number(newX.toFixed(1)); // use a precision of 1
			newY = Number(newY.toFixed(1)); // use a precision of 1
			
			comp.x = newX;
			comp.y = newY;
			comp.rotation = rotateValue;
		}
		
		public static function setBitmapAlpha(bmd:BitmapData, alpha:Number):void
		{
			var ct:ColorTransform = new ColorTransform();
			ct.alphaMultiplier = alpha; 
			bmd.colorTransform(bmd.rect, ct);
		}

	}
}